﻿Partial Public Class AuctionDetails
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim db As New AuctionInfoDataContext

        Dim auctionQuery = From au In db.Auctions _
                    Where au.auctionID = Request.QueryString("auctionID") _
                    Select au

        Dim auctionBid = From bid In db.Bids _
                          Where bid.auctionID = Request.QueryString("auctionId") _
                          Select bid Order By bid.bidAmount Descending

        Dim bidCount = (From bd In db.Bids _
                     Where bd.auctionID = Request.QueryString("auctionID") _
                     Select bd).Count

        Dim shipMethod = From method In db.Shippings _
                         Where method.AuctionID = Request.QueryString("auctionid") _
                         Select method

        Dim ownerRating = From rating In db.Feedbacks _
                          Where rating.auctionID = Request.QueryString("auctionId") _
                          Select rating


        If ownerRating.Count > 0 Then
            Dim ownerAverage As Decimal
            For Each rating In ownerRating
                ownerAverage += rating.score
            Next
            ownerAverage = (ownerAverage / ownerRating.Count)
            label02.Text = "Auction By " & Membership.GetUser(auctionQuery.First.ownerID).UserName & "<br> rating " & ownerAverage
        Else
            label02.Text = "Auction By " & Membership.GetUser(auctionQuery.First.ownerID).UserName & "<br> rating [unrated]"
        End If

        With auctionQuery.FirstOrDefault
            label1.Text = .auctionID
            label2.Text = .title
            label3.Text = .subTitle
        End With

        If auctionBid.Count = 0 Then
            label4.Text = "No Bids"
        Else
            label4.Text = Format(auctionBid.FirstOrDefault.bidAmount, "$0.00")
        End If

        If bidCount = 1 Then
            label5.Text = bidCount & " bid"
            linkbutton1.Text = "View History"
        ElseIf bidCount > 0 Then
            label5.Text = bidCount & " bids"
            linkbutton1.Text = "View History"
        End If


        If bidCount > 0 Then
            label6.Text = Membership.GetUser(auctionBid.FirstOrDefault.ownerID).UserName
        Else
            label6.Text = "N / A"
        End If


        With shipMethod.FirstOrDefault
            label7.Text = .Method
            label8.Text = Format(.Price, "$0.00")
            label9.Text = .DeliveryTime
            label10.Text = .Restrictions
        End With

        label11.Text = auctionQuery.FirstOrDefault.description

        Dim bidHistory = From bh In db.Bids _
                         Where bh.auctionID = Request.QueryString("auctionID") _
                         Select bidder = Membership.GetUser(bh.ownerID).UserName, bidAmount = bh.bidAmount

        repeater1.DataSource = bidHistory
        repeater1.DataBind()
    End Sub

    Protected Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
        Dim db As New AuctionInfoDataContext
        Dim newBid As New Bid
        If Not String.IsNullOrEmpty(textbox1.Text) Then
            newBid.bidAmount = textbox1.Text
        End If
        newBid.auctionID = Request.QueryString("auctionId")
        newBid.ownerID = Membership.GetUser().ProviderUserKey
        db.Bids.InsertOnSubmit(newBid)
        db.SubmitChanges()

        Response.Redirect(Request.FilePath & "?auctionID=" & Request.QueryString("auctionId"))
    End Sub
End Class